﻿/* Copyright (c) 2008, 2009, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class Assembler
    {
        bool is_assignment_compatible(Signature.Param dest, Signature.Param src)
        { return is_assignment_compatible(dest.Type, src.Type); }
        bool is_assignment_compatible(Signature.BaseOrComplexType dest, Signature.BaseOrComplexType src)
        {
            /* See CIL I:8.7 for the definition of assignment-compatible */

            if (dest.GetType() != src.GetType())
                return false;
            if (dest is Signature.BaseType)
            {
                Signature.BaseType btd = dest as Signature.BaseType;
                Signature.BaseType bts = src as Signature.BaseType;

                if (btd.Type == bts.Type)
                    return true;

                if (((btd.Type == BaseType_Type.U1) && (bts.Type == BaseType_Type.I1)) ||
                    ((btd.Type == BaseType_Type.I1) && (bts.Type == BaseType_Type.U1)))
                    return true;
                if (((btd.Type == BaseType_Type.U2) && (bts.Type == BaseType_Type.I2)) ||
                    ((btd.Type == BaseType_Type.I2) && (bts.Type == BaseType_Type.U2)))
                    return true;
                if (((btd.Type == BaseType_Type.U4) && (bts.Type == BaseType_Type.I4)) ||
                    ((btd.Type == BaseType_Type.I4) && (bts.Type == BaseType_Type.U4)))
                    return true;
                if (((btd.Type == BaseType_Type.U8) && (bts.Type == BaseType_Type.I8)) ||
                    ((btd.Type == BaseType_Type.I8) && (bts.Type == BaseType_Type.U8)))
                    return true;
                if (((btd.Type == BaseType_Type.U) && (bts.Type == BaseType_Type.I)) ||
                    ((btd.Type == BaseType_Type.I) && (bts.Type == BaseType_Type.U)))
                    return true;

                return false;
            }
            if (dest is Signature.ZeroBasedArray)
            {
                Signature.ZeroBasedArray zbd = dest as Signature.ZeroBasedArray;
                Signature.ZeroBasedArray zbs = src as Signature.ZeroBasedArray;
                return is_assignment_compatible(zbd.ElemType, zbs.ElemType);
            }
            if (dest is Signature.ComplexType)
            {
                Signature.ComplexType ctd = dest as Signature.ComplexType;
                Signature.ComplexType cts = src as Signature.ComplexType;

                if (ctd.Type.Value == cts.Type.Value)
                    return true;
                if (can_cast(ctd.Type, cts.Type))
                    return true;
                else
                    return false;
            }
            if (dest is Signature.GenericType)
            {
                Signature.GenericType gtd = dest as Signature.GenericType;
                Signature.GenericType gts = dest as Signature.GenericType;

                if (!is_assignment_compatible(gtd.GenType, gts.GenType))
                    return false;
                if (gtd.GenParams.Count != gts.GenParams.Count)
                    return false;
                for (int i = 0; i < gtd.GenParams.Count; i++)
                {
                    if (!is_assignment_compatible(gtd.GenParams[i], gts.GenParams[i]))
                        return false;
                }
                return true;
            }
            throw new NotSupportedException();
        }

        bool can_cast(Signature.BaseOrComplexType dest, Signature.BaseOrComplexType src)
        {
            if ((dest is Signature.BaseType) && (src is Signature.BaseType))
                return can_cast(dest as Signature.BaseType, src as Signature.BaseType);
            if ((dest is Signature.ZeroBasedArray) && (src is Signature.ZeroBasedArray))
                return can_cast(((Signature.ZeroBasedArray)dest).ElemType, ((Signature.ZeroBasedArray)src).ElemType);
            return can_cast(Metadata.GetTypeDef(dest, this), Metadata.GetTypeDef(src, this));    
        }

        bool can_cast(Signature.BaseType dest, Signature.BaseType src)
        {
            if (dest.Type == src.Type)
                return true;
            return false;
        }

        bool can_cast(Token dest, Token src)
        { return can_cast(Metadata.GetTypeDef(dest, this), Metadata.GetTypeDef(src, this)); }

        bool can_cast(Metadata.TypeDefRow tdd, Metadata.TypeDefRow tds)
        {
            if (tdd == tds)
                return true;
            if (tds.Extends.Value != null)
                return can_cast(tdd, Metadata.GetTypeDef(tds.Extends.Value, this));
            return false;
        }
    }
}
